<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>添加访客</title>

    <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0.maximum-scale=1.0,user-scalable=no"
        name="viewport">

    <link rel="stylesheet" href="css/reset.css">

    <link rel="stylesheet" href="css/bossIndex.css">

    <link rel="stylesheet" href="css/flexible.css">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.12/lib/index.css">

    <script src="js/fontsize.js"></script>

    <script src="js/vue/vue.js"></script>

    <script src="js/jquery-3.3.1.min.js"></script>

    <script src="js/fontsize.js"></script>

    <script src="js/vue/axios.js"></script>

    <script src="js/jquery.md5.js"></script>

    <script src="js/glabe.js"></script>

    <script src="js/vue/qs.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/vant@2.12/lib/vant.min.js"></script>


    <style>
        .container_box {

            font-size: 0.14rem;

        }
    </style>

</head>

<body>

    <div class="container_box" id="app">

        <template>

            <!-- <div class="Information_entry">

            <label class="lf">访客类别</label>

            <div class="rf message" @click="userTypeShow = true">

                <span>{{userType ? userType.Utname : '选择访客类别'}}</span>

                <img src="./images/right_icon.png" alt="">

            </div>

            <van-popup 

            v-model="userTypeShow"

            position="bottom"

            >
            
                <van-picker show-toolbar :columns="UsertypeList" value-key="Utname" @confirm="UsertypeSelect" @cancel="userTypeShow = false"/>

            </van-popup>

        </div> -->

            <div class="Information_entry">

                <label class="lf">姓名</label>

                <div class="rf message padding14"><input placeholder="请输入会员姓名" v-model="Username"></div>

            </div>


            <div class="Information_entry">

                <label class="lf">电话</label>

                <div class="rf message padding14">

                    <input type="text" oninput="value=value.replace(/[^\d]/g,'')" maxlength="11" placeholder="请输入会员手机号码"
                        v-model="Phone">

                </div>

            </div>


            <div class="Information_entry">

                <label class="lf">性别</label>

                <div class="rf sex padding14">

                    <label>

                        <input type=radio name=x1 :checked="Sex == 1 ? 'checked' : '' "
                            @click="getRadioVal(1)"><span>男</span>

                    </label>

                    <label>

                        <input type=radio name=x1 :checked="Sex == 0 ? 'checked' : '' "
                            @click="getRadioVal(0)"><span>女</span>

                    </label>

                </div>

            </div>

            <!-- 出生日期 -->


            <div class="Information_entry" @click="selectcsny = true">
                <label class="lf">出生日期</label>

                <div class="rf message">

                    <span>{{selectDate ? selectDate : '请选择出生日期'}}</span>

                    <img src="./images/right_icon.png" alt="">

                </div>
            </div>


            <van-popup v-model="selectcsny" position="bottom">

                <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate"
                    :max-date="maxDate" @confirm="onConfirm" @cancel="selectcsny = false" />

            </van-popup>


            <div class="Information_entry">

                <label class="lf">来源</label>

                <div class="rf message" @click="cfromShow = true">

                    <span>{{cfrom ? cfrom.Fromname : '选择来源'}}</span>

                    <img src="./images/right_icon.png" alt="">

                </div>

                <van-popup v-model="cfromShow" position="bottom">

                    <van-picker show-toolbar :columns="cfromList" value-key="Fromname" @confirm="cfromSelect"
                        @cancel="cfromShow = false" />

                </van-popup>

            </div>


            <div class="Information_entry">

                <label class="lf">会籍顾问</label>

                <div class="rf message" @click="staffShow = true">

                    <span>{{staff ? staff.Sname : '选择会籍顾问'}}</span>

                    <img src="./images/right_icon.png" alt="">

                </div>

                <van-popup v-model="staffShow" position="bottom">

                    <van-picker show-toolbar :columns="staffList" value-key="Sname" @confirm="staffSelect"
                        @cancel="staffShow = false" />

                </van-popup>

            </div>

            <!-- <div class="Information_entry">

                <label class="lf">访客星级</label>

                <div class="rf sex padding14">

                    <van-rate v-model="Level" />

                </div>

            </div> -->
            <div class="Information_entry">

                <label class="lf">教练</label>

                <div class="rf message" @click="jlstaffShow = true">

                    <span>{{jlstaff ? jlstaff.Sname : '选择教练'}}</span>

                    <img src="./images/right_icon.png" alt="">

                </div>

                <van-popup v-model="jlstaffShow" position="bottom">

                    <van-picker show-toolbar :columns="staffList" value-key="Sname" @confirm="jlSelect"
                        @cancel="staffShow = false" />

                </van-popup>

            </div>


        </template>

        <div class="save">

            <button type="button" @click="submitData()">提交</button>

        </div>

    </div>

</body>

<script>

    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            S: this.getMilliseconds(), //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
                );
        return fmt;
    };
    var app = new Vue({

        el: '#app',

        data: {
            minDate: new Date(1940, 0, 1),
            maxDate: new Date(),
            currentDate: new Date(2000, 0, 1),
            selectDate: null,
            selectcsny: false,

            id: null,

            //访客弹出成

            userTypeShow: false,

            //访客类别列表

            UsertypeList: null,

            //选中访客

            userType: null,

            //维护人弹出层

            staffShow: false,

            //维护人列表

            staffList: null,

            //选中维护人

            staff: null,

            //选中教练
            jlstaff: null,

            jlstaffShow: false,
            //来源弹出成

            cfromShow: false,

            //来源列表

            cfromList: null,

            //选中来源

            cfrom: null,

            //客户姓名

            Username: null,

            //客户性别

            Sex: 1,

            //访客星级

            Level: 1,

            //电话号码

            Phone: null

        },

        created() {

            //获取路由参数

            let id = getUrlQueryString('id');

            //判断是否修改

            if (id) {

                //修改

                this.id = id;

                //获取客户详细信息

                axios.get(`mobileapi/boss/customer.php?act=details&uid=${id}`).then(res => {

                    if (res.data.code == 200) {

                        let Usertype = res.data.data.Usertype;

                        this.UsertypeList = Usertype;

                        let staff = res.data.data.staff;

                        this.staffList = staff;

                        let cfrom = res.data.data.cfrom;

                        this.selectDate = res.data.data.info.Birthday;


                        this.cfromList = cfrom;

                        //客户类型

                        Usertype.forEach(item => {

                            if (item.id == res.data.data.info.Usertype) {

                                this.userType = item;

                            }

                        });

                        //维护人

                        staff.forEach(item => {

                            if (item.id == res.data.data.info.Coach) {

                                this.jlstaff = item;

                            }

                        });
                        staff.forEach(item => {

                            if (item.id == res.data.data.info.Sale) {

                                this.staff = item;

                            }

                        });
                        //客户来源

                        cfrom.forEach(item => {

                            if (item.id == res.data.data.info.Userfrom) {

                                this.cfrom = item;

                            }

                        });

                        this.Username = res.data.data.info.Username;

                        this.Phone = res.data.data.info.Phone;

                        this.Sex = res.data.data.info.Sex;

                        this.Level = res.data.data.info.Level;

                    } else {

                        vant.Toast(res.data.msg);

                    }

                }).catch(err => {

                    vant.Toast('请求发送失败，请重试')

                })

            } else {

                //新增

                //获取下拉数据

                axios.get('mobileapi/boss/customer.php?act=add').then(res => {

                    if (res.data.code == 200) {

                        this.UsertypeList = res.data.data.Usertype;

                        this.cfromList = res.data.data.cfrom;

                        this.staffList = res.data.data.staff;

                    } else {

                        vant.Toast(res.data.msg);

                    }

                }).catch(err => {

                    vant.Toast('请求发送失败，请重试');

                })

            }

        },

        methods: {
            onConfirm(value) {

                this.selectDate = value.format("yyyy-MM-dd");
                this.selectcsny = false;
            },
            getRadioVal(val) {

                console.log(val);

                this.Sex = val;

                console.log(this.Sex);

            },

            //访客选中

            UsertypeSelect(obj) {

                this.userType = obj;

                this.userTypeShow = false;

            },

            //来源确认

            cfromSelect(obj) {

                this.cfrom = obj;

                this.cfromShow = false;

            },

            //维护人确认

            staffSelect(obj) {

                console.log(obj);

                this.staff = obj;

                this.staffShow = false;

            },


            //教练选择

            jlSelect(obj) {

                console.log(obj);

                this.jlstaff = obj;

                this.jlstaffShow = false;

            },


            //提交按钮

            submitData() {



                if (this.Username == null || this.Username == '') {

                    vant.Toast('请输入姓名');

                    return false;

                }

                if (this.Phone == null || this.Phone == '') {

                    vant.Toast('请输入电话');

                    return false;

                }


                if (this.Sex === null || this.Sex === '') {

                    vant.Toast('请选择性别');

                    return false;

                }

                if (this.selectDate == null || this.selectDate == '') {

                    vant.Toast('请选择出生日期');

                    return false;

                }


                if (this.cfrom == null || this.cfrom == '') {

                    vant.Toast('请选择来源');

                    return false;

                }


                if (this.staff == null || this.staff == '') {

                    vant.Toast('请选择会籍顾问');

                    return false;

                }

                if (this.jlstaff == null || this.jlstaff == '') {

                    vant.Toast('请选择教练');

                    return false;

                }
                //整合数据

                if (this.Sex == 1) {

                    this.Sex = '1'

                } else {

                    this.Sex = '0'

                }
                console.log(this.id);

                if (this.id != null && this.id.length > 0) {

                    //修改

                    let from = JSON.stringify({

                        Username: this.Username,
                        Sex: this.Sex,
                        Sale: this.staff.id,
                        Coach: this.jlstaff.id,
                        Level: this.Level,
                        Phone: this.Phone,
                        Userfrom: this.cfrom.id,
                        Uid: this.id,
                        Birthday: this.selectDate
                    });
                    let sign = md5(from);

                    let params = {

                        from,

                        sign

                    }

                    axios.post('mobileapi/boss/customer.php?act=editgo', Qs.stringify(params)).then(res => {

                        if (res.data.code == 200) {

                            vant.Toast('客户信息修改成功');

                            setTimeout(() => {

                                location.href = 'visitorDetails.html?id=' + this.id + '&active=2';

                            }, 1500)

                        } else {

                            vant.Toast(res.data.msg);

                        }

                    }).catch(err => {

                        vant.Toast('请求发送失败。请重新获取')

                    })

                } else {

                    //新增

                    let from = JSON.stringify({


                        Username: this.Username,

                        Sex: this.Sex,

                        Sale: this.staff.id,

                        Level: this.Level,

                        Phone: this.Phone,

                        Userfrom: this.cfrom.id,
                        Birthday: this.selectDate,
                        Coach: this.jlstaff.id

                    });

                    let sign = md5(from);

                    let params = {

                        from,

                        sign

                    }

                    axios.post('mobileapi/boss/customer.php?act=addgo', Qs.stringify(params)).then(res => {

                        if (res.data.code == 200) {

                            vant.Toast('客户添加成功');

                            setTimeout(() => {

                                location.href = 'visitor.html';

                            }, 1500)

                        } else {

                            vant.Toast(res.data.msg);

                        }

                    }).catch(err => {

                        vant.Toast('请求发送失败。请重新获取')

                    })

                }





            }

        },

    })

</script>

</html>